package com.sakura.mall.member.memberCollectSubject.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.member.memberCollectSubject.domain.entity.MemberCollectSubjectEntity;
import com.sakura.mall.member.memberCollectSubject.domain.request.AddMemberCollectSubjectRequest;
import com.sakura.mall.member.memberCollectSubject.domain.request.UpdateMemberCollectSubjectRequest;
import com.sakura.mall.member.memberCollectSubject.domain.request.MemberCollectSubjectPageRequest;
import com.sakura.mall.member.memberCollectSubject.domain.vo.MemberCollectSubjectVo;
import com.sakura.mall.member.memberCollectSubject.mapper.MemberCollectSubjectMapper;
import com.sakura.mall.member.memberCollectSubject.service.MemberCollectSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员收藏的专题活动 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class MemberCollectSubjectServiceImpl extends ServiceImpl<MemberCollectSubjectMapper, MemberCollectSubjectEntity> implements MemberCollectSubjectService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddMemberCollectSubjectRequest request) {
        // todo 其他校验逻辑
        MemberCollectSubjectEntity memberCollectSubject = request.convert(MemberCollectSubjectEntity.class);
        this.save(memberCollectSubject);
    }

    @Override
    public Page<MemberCollectSubjectVo> queryPage(MemberCollectSubjectPageRequest request) {
        IPage<MemberCollectSubjectEntity> qp = request.toPage();
        LambdaQueryWrapper<MemberCollectSubjectEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<MemberCollectSubjectVo> ret = qp.convert(entity -> entity.convert(MemberCollectSubjectVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateMemberCollectSubjectRequest request) {
        MemberCollectSubjectEntity entity = request.convert(MemberCollectSubjectEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

